<?php
/**
 * 后端操作
 * Date: 2019/1/29 0029
 */
//登录
function dologin(){
    $username = isset($_POST['username'])?$_POST['username']:'';
    $password = isset($_POST['password'])?md5($_POST['password']):'';

    $where = "username='$username' AND password='$password'";
    $row = find('lyy_member','*',$where);

    if ($row){
        $_SESSION['name'] = $row['username'];
        $_SESSION['id'] = $row['id'];
        $_SESSION['identity'] = $row['identity'];
        return $_SESSION['identity'];
    }else{
        return 400;
    }
}
//注销
function dologout(){
    $_SESSION = array();
    session_destroy();//销毁session
    header('location:index.php');//跳转页面
}
//注册
function register(){
    $arr = $_POST;#获取前端传过来的数据
    $arr['password'] = md5($_POST['password']);#密码
    $arr['createtime'] = time();#时间
    $arr['identity'] = 2;#角色身份

    if (insert('lyy_member',$arr)){#调用insert方法，插入数据库
        return 200;
    }else{
        return "失败";
    }
}
//修改密码
function edit_pwd(){
    $arr = $_POST;#获取参数
    $arr['oldpwd'] = md5($arr['oldpwd']);#旧密码加密

    $where = "id=".$_SESSION['id'];#条件
    $row = find('lyy_member','*',$where);#查询该用户的id

    if ($row){
        if ($row['password']==$arr['oldpwd']){
            $data = ['password'=>md5($arr['newpwd'])];
            $edit = update('lyy_member',$data,$where);#修改
            if ($edit){
                $_SESSION = array();
                session_destroy();//销毁session
                return 200;
            }
        }
    }
    return '修改密码失败';
}
//发布公告
function add_gonggao(){
    $arr = $_POST;#获取参数
    $arr['createtime'] = time();#时间
    $arr['updatetime'] = time();#时间
    $arr['uid'] = $_SESSION['id'];#获取用户id
    if (insert('lyy_gonggao',$arr)){#调用insert方法，插入数据库
        return 200;
    }else{
        return "发布失败";
    }
}
//修改公告
function update_gonggao(){
    $arr = $_POST;
    $arr['updatetime'] = time();
    $arr['uid']=$_SESSION['id'];
    $res = update('lyy_gonggao',$arr,'id='.$arr['id']);
    if ($res){
        return 200;
    }else{
        return '修改失败，该条公告已存在';
    }
}
//文件上传
function up_file(){
    $data = uploadFile('./uploads');
    if ($data[0]['error']==0){
        $res = [
            'name'=>'uploads'.'/'.date('Ym').'/'.$data[0]['name'],
            'code'=>200  #200表示成功，也可以用别的数表示，一般用200
        ];
        return json_encode($res);
    }else{
        return false;
    }

}
//添加实验、资料、练习
function add_kecheng(){
    $arr = $_POST;
    $arr['createtime'] = time();
    $arr['uid']=$_SESSION['id'];
    $table = $arr['table'];
    unset($arr['table']);
    $res = insert($table,$arr);
    if ($res){
        return 200;
    }else{
        //添加失败，删除已上传的文件
        if (file_exists("../uploads/".date('Ym').$arr['file_url'])){
            unlink("../uploads/".date('Ym').$arr['file_url']);
        }
        return '添加失败，课程名称已存在';
    }
}
//修改实验、资料、练习
function update_kecheng(){
    $arr = $_POST;
    $arr['updatetime'] = time();
    $arr['uid']=$_SESSION['id'];
    $table = $arr['table'];
    unset($arr['table']);
    $res = update($table,$arr,'id='.$arr['id']);
    if ($res){
        return 200;
    }else{
        //添加失败，删除已上传的文件
        if (file_exists("../uploads/".date('Ym').$arr['file_url'])){
            unlink("../uploads/".date('Ym').$arr['file_url']);
        }
        return '修改失败，课程名称已存在';
    }
}
//删除实验、资料、练习、留言、公告、用户
function del(){
    $arr = $_POST;
    $arr['updatetime'] = time();
    $table = $arr['table'];
    unset($arr['table']);
    $res = update($table,$arr,'id='.$arr['id']);
    if ($res){
        return 200;
    }
    return '删除失败';
}
//添加留言、回复留言
function add_reply(){
    if ($_SESSION){
        $arr = $_POST;
        $arr['createtime'] = time();
        $arr['uid']=$_SESSION['id'];
        $res = insert('lyy_message',$arr);
        if ($res){
            return 200;
        }else{
            return '操作失败';
        }
    }else{
        return '您还没有登录,请登录后操作';
    }

}
//找回密码
function find_pwd(){
    $arr = $_POST;#获取参数

    $where = "username='".$arr['username']."' and email='".$arr['email']."'";#条件
    $row = find('lyy_member','id',$where);#查询该用户的id

    if ($row){
        $data = ['password'=>md5($arr['password'])];#密码加密
        $edit = update('lyy_member',$data,$row['id']);#修改
        if ($edit){
            return 200;
        }
    }
    return 400;
}